package org.example.test.service.impl;

import org.example.test.dal.entity.Comments;
import org.example.test.dal.mapper.CommentsMapper;
import org.example.test.model.CommentsModel;
import org.example.test.service.CommentsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;

@Component
public class CommentsServiceImpl implements CommentsService {

    private static final Logger LOGGER = LoggerFactory.getLogger(CommentsServiceImpl.class);

    @Autowired
    private CommentsMapper commentMapper;

    @Override
    public void createComment(CommentsModel commentRequest) {
        try {
            Comments comments = new Comments();
            comments.setContent(commentRequest.getContent());
            comments.setProductId(commentRequest.getProductId());
            comments.setUserId(commentRequest.getUserId());
            commentMapper.insert(comments);
        } catch (Exception e) {
            LOGGER.error("Error creating comment", e);
            throw new RuntimeException("Error creating comment: " + e.getMessage());
        }
    }

    @Override
    public void deleteComment(Long commentId) {
        try {
            commentMapper.deleteByPrimaryKey(commentId);
        } catch (Exception e) {
            LOGGER.error("Error deleting comment", e);
            throw new RuntimeException("Error deleting comment: " + e.getMessage());
        }
    }

    @Override
    public CommentsModel getCommentById(Long commentId) {
        try {
            Comments comment = commentMapper.selectByPrimaryKey(commentId);
            if (comment == null) {
                throw new RuntimeException("Comment not found");
            }

            CommentsModel commentModel = new CommentsModel();
            BeanUtils.copyProperties(comment, commentModel);
            return commentModel;
        } catch (Exception e) {
            LOGGER.error("Error getting comment", e);
            throw new RuntimeException("Error getting comment: " + e.getMessage());
        }
    }

    @Override
    public List<CommentsModel> listCommentsByUser(Long userId) {
        try {
            List<Comments> comments = commentMapper.selectByUserId(userId);
            List<CommentsModel> commentModels = new ArrayList<>();
            for (Comments comment : comments) {
                CommentsModel commentModel = new CommentsModel();
                BeanUtils.copyProperties(comment, commentModel);
                commentModels.add(commentModel);
            }
            return commentModels;
        } catch (Exception e) {
            LOGGER.error("Error listing comments", e);
            throw new RuntimeException("Error listing comments: " + e.getMessage());
        }
    }

    @Override
    public List<CommentsModel> listCommentsByProduct(Long productId) {
        try {
            List<Comments> comments = commentMapper.selectByProductId(productId);
            List<CommentsModel> commentModels = new ArrayList<>();
            for (Comments comment : comments) {
                CommentsModel commentModel = new CommentsModel();
                BeanUtils.copyProperties(comment, commentModel);
                commentModels.add(commentModel);
            }
            return commentModels;
        } catch (Exception e) {
            LOGGER.error("Error listing comments", e);
            throw new RuntimeException("Error listing comments: " + e.getMessage());
        }
    }
}
